//下载图片
export const downloadImg = (url: string, name?: string) => {
  let image = new Image();
  image.setAttribute('crossOrigin', 'anonymous'); // 处理跨域
  image.src = url;
  image.onload = () => {
    let canvas = document.createElement('canvas');
    canvas.width = image.width;
    canvas.height = image.height;
    let ctx = canvas.getContext('2d');
    ctx.drawImage(image, 0, 0, image.width, image.height);
    canvas.toBlob((blob) => {
      let url = URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = url;
      link.download = name || url;
      link.click(); // 触发下载
      URL.revokeObjectURL(url); // 释放之前创建的对象URL
    });
  };
};
import { formatCurrentTime } from '@/utils/moment.ts';
//下载文件
export const downloadByBlob = (res: any, fileName: string, type: string = 'xlsx') => {
  const blob = new Blob([res]); //处理文档流
  let objectUrl = URL.createObjectURL(blob);
  const elink = document.createElement('a');
  elink.download = `${fileName}${formatCurrentTime()}.${type}`; //下载文件名称,
  elink.style.display = 'none';
  elink.href = objectUrl;
  document.body.appendChild(elink);
  elink.click();
  URL.revokeObjectURL(elink.href); // 释放URL 对象
  document.body.removeChild(elink);
};
//下载本地文件
export const downloadFile = (fileName: string, type: string = 'xlsx', url?: string) => {
  const link = document.createElement('a');
  link.href = url || `/${fileName}.${type}`; // public 文件夹下的文件路径
  link.download = `${fileName}${formatCurrentTime()}.${type}`; // 下载时使用的文件名
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};
